package com.scqkxx.gamebox.activity.myself;

import android.app.Dialog;
import android.content.ContentValues;
import android.content.Intent;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;

import com.avos.avoscloud.AVException;
import com.avos.avoscloud.AVObject;
import com.avos.avoscloud.AVQuery;
import com.avos.avoscloud.AVUser;
import com.avos.avoscloud.FindCallback;
import com.avos.avoscloud.SaveCallback;
import com.scqkxx.gamebox.R;
import com.scqkxx.gamebox.activity.activities.utils.ActivityUtil;
import com.scqkxx.gamebox.activity.activities.utils.GlideUtils;
import com.scqkxx.gamebox.activity.activities.view.TitleView;
import com.scqkxx.gamebox.activity.myself.spaceUtils.SpaceUtils;
import com.scqkxx.gamebox.bean.EvenBusBean;
import com.scqkxx.gamebox.databinding.ActivityMyselfEditSignatureBinding;
import com.scqkxx.gamebox.databinding.ActivityMyselfInforBinding;
import com.scqkxx.gamebox.utils.DialogUtils;
import com.scqkzqtz.base.base.BaseActivity;
import com.scqkzqtz.base.library.utils.ToastUtils;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import java.text.SimpleDateFormat;
import java.util.List;

/**
 * 物品详情
 * Created by hef on 2018/8/13.
 */

public class MyselfEditSignatureActivity extends BaseActivity<ActivityMyselfEditSignatureBinding> implements View.OnClickListener {
    private String objectId;
    private int belongGroupId;
    private int spaceId;
    private View inflate;
    private Button choosePhoto;
    private Button takePhoto;
    private Button cancel;
    private Dialog dialog;
    private AVObject avObject;
    private String imageUrl;
    private String spaceName;
    private String goodsName;
    private boolean isPblic;

    @Override
    public int setLayoutId() {
        return R.layout.activity_myself_edit_signature;
    }

    @Override
    public void init() {
        EventBus.getDefault().register(this);
        objectId = getIntent().getStringExtra("objectId");
        belongGroupId = getIntent().getIntExtra("belongGroupId", 0);
        spaceId = getIntent().getIntExtra("spaceId", 0);
        binding.tvTitleView.getIv_setting().setImageResource(R.mipmap.space_edit_icon);
        binding.tvTitleView.setOnTitleClickListener(mTitleType -> {
            if (TitleView.CLICK_BACK == mTitleType) {
                finish();
            } else if (TitleView.CLICK_SETTING == mTitleType) {
                inflate = LayoutInflater.from(MyselfEditSignatureActivity.this).inflate(R.layout.eait_show_dialog, null);
                dialog = DialogUtils.showBottonDialog(MyselfEditSignatureActivity.this, inflate);
                choosePhoto = (Button) inflate.findViewById(R.id.choosePhoto);
                takePhoto = (Button) inflate.findViewById(R.id.takePhoto);
                cancel = (Button) inflate.findViewById(R.id.btn_cancel);
                takePhoto.setVisibility(View.GONE);
                choosePhoto.setOnClickListener(MyselfEditSignatureActivity.this);
//                takePhoto.setOnClickListener(MyselfEditSignatureActivity.this);
                cancel.setOnClickListener(MyselfEditSignatureActivity.this);
                dialog.show();
            }
        });

        binding.tvEdit.setOnClickListener(v -> {
            //编辑
            ContentValues contentValues = new ContentValues();
            contentValues.put("spaceState", 6 );
            contentValues.put("imageUrl", (String) avObject.get("avatar"));
            contentValues.put("spaceName", (String) avObject.get("positionName"));
            contentValues.put("goodsName", (String) avObject.get("name"));
            contentValues.put("isPblic", ((Integer) avObject.get("isPublic")) == 0 ? false : true);
            contentValues.put("note", (String) avObject.get("note"));
            contentValues.put("objectId", objectId);
            ActivityUtil.switchTo(MyselfEditSignatureActivity.this, MyselfInforActivity.class, contentValues);
        });

        initData();
    }

    private void initData() {
        DialogUtils.showDialog(this);
        AVQuery<AVObject> avQuery = new AVQuery("Goods");
        avQuery.whereEqualTo("objectId", objectId);
        avQuery.whereEqualTo("isDisable", 0);
        avQuery.findInBackground(new FindCallback<AVObject>() {
            @Override
            public void done(List<AVObject> avObjects, AVException avException) {
                if (avException == null) {
                    Log.e("TAG", avObjects.toString());
                    if (avObjects.size() > 0) {
                        avObject = avObjects.get(0);
                        GlideUtils.loadImage(MyselfEditSignatureActivity.this, avObject.getString("avatar"), binding.goodsIamge);
                        binding.GoodsName.setText(avObject.getString("name"));
                        binding.createTime.setText(new SimpleDateFormat("yyyy年MM月dd日 HH:mm").format(avObject.getCreatedAt()));
                        binding.spaceName.setText("空间 : " + avObject.get("spaceName"));
                        binding.tvSign.setText(avObject.get("marksNum") + "人标记");
                        binding.tvDynamic.setText(avObject.get("newsNum") + "条动态");
                        binding.tvRemarks.setText(avObject.getString("note"));
                    }
                    DialogUtils.dismiss();
                }
            }
        });
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.choosePhoto:
                //删除
                if (dialog != null) dialog.dismiss();
                avObject.put("isDisable" ,1);
                avObject.saveInBackground(new SaveCallback() {
                    @Override
                    public void done(AVException e) {
                        if (e == null){
                            SpaceUtils.sendNotify(3,MyselfEditSignatureActivity.this );
                        }else{
                            ToastUtils.showShortToast(MyselfEditSignatureActivity.this , "删除失败..");
                        }
                    }
                });
                break;
            case R.id.btn_cancel:
                if (dialog != null) dialog.dismiss();
                break;
        }
    }

    @Subscribe
    public void changeDataBean(EvenBusBean eventBus) throws AVException {
        if (eventBus.getState() == 101) {
            initData();
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
